package com.ysx.dfs.controller;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dfs")
public class ParsingPathController {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;

    @PostMapping("/parsingPath")//TODO
    List<byte[]> parsingPathByListStr(@RequestBody List<String> listStr) {
        List<byte[]> listUrl = new ArrayList<>();
        //遍历集合
        for (String imageUrl : listStr) {
            StorePath storePath = StorePath.parseFromUrl(imageUrl);
            byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadCallback<byte[]>() {
                @Override
                public byte[] recv(InputStream inputStream) throws IOException {
                    //将字节流转成byte
                    byte[] bytes = IOUtils.toByteArray(inputStream);
                    return bytes;
                }
            });
            //返回
            listUrl.add(bytes);
        }
        return listUrl;
    }

}
